package com.framework.service.core.controller;

import com.framework.common.controller.BaseController;
import com.framework.common.domain.R;
import com.framework.service.core.service.ICommonSdkService;
import com.framework.service.core.service.IFileService;
import com.framework.core.api.response.IdentityRes;
import com.framework.service.core.service.ICommonSdkService;
import com.framework.service.core.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/4/26 22:11
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/common")
@Api(value = "CommonController", description = "通用sdk服务接口")
public class CommonController extends BaseController {

    @Autowired
    private ICommonSdkService iCommonService;

    @Autowired
    private IFileService fileService;

    @ApiOperation(value = "验证身份证信息", httpMethod = "GET")
    @RequestMapping(value = "/identityCard",method = RequestMethod.GET)
    public String identityCard(@RequestParam String cardNo) {
        return iCommonService.identityCardByAliyun(cardNo);
    }

    @ApiOperation(value = "发送手机短信", httpMethod = "GET")
    @RequestMapping(value = "/sendSms",method = RequestMethod.GET)
    public String sendSms(@RequestParam("phone") String phone, @RequestParam("code") String code,
                          @RequestParam("params") String params, @RequestParam("skin") String skin,
                          @RequestParam("name") String name) {
        if(StringUtils.isNotBlank(name) && StringUtils.equals("code6", name)) {
            return iCommonService.sendMessageForCodeByAliyun(phone, code);
        }
        return iCommonService.sendMessageByAliyun(phone, params, skin);
    }

    @ApiOperation(value = "识别身份证照片", httpMethod = "POST")
    @RequestMapping(value = "/identityCardByBaidu",method = RequestMethod.POST)
    public R identityCardByBaidu(@ApiParam(value = "身份证图片base64信息") String cardImg,
                                      @ApiParam(value = "身份证正or反面") String imgSide) {
        return iCommonService.identityCardByBaidu(cardImg, imgSide);
    }

    @ApiOperation(value = "识别营业执照照片", httpMethod = "POST")
    @RequestMapping(value = "/identityBizLicenseByBaidu",method = RequestMethod.POST)
    public R identityLicenseByBaidu(@ApiParam(value = "营业执照base64信息") String licenseImg) {
        return iCommonService.identityBusinessByBaidu(licenseImg);
    }

    @ApiOperation(value = "识别身份证照片并上传", httpMethod = "POST")
    @RequestMapping(value = "/uploadAndIdentityCardByBaidu",method = RequestMethod.POST)
    @ResponseBody
    public IdentityRes identityCardByBaidu(@ApiParam(value = "身份证图片") MultipartFile file,
                                           @ApiParam(value = "身份证正or反面") String imgSide) {
        return iCommonService.identityCardByBaidu(file, imgSide);
    }

    @ApiOperation(value = "识别营业执照照片并上传", httpMethod = "POST")
    @RequestMapping(value = "/uploadAndIdentityBizLicenseByBaidu",method = RequestMethod.POST)
    @ResponseBody
    public IdentityRes identityLicenseByBaidu(@ApiParam(value = "营业执照图片") MultipartFile file) {
        return iCommonService.identityBusinessByBaidu(file);
    }

    @ApiOperation(value = "在线显示图片", httpMethod = "GET")
    @RequestMapping("/showImage")
    @ResponseBody
    public void showImage(@ApiParam(value = "图片名称") String fileName,HttpServletResponse response) {
        fileService.downloadFileFromOss(fileName, response);
    }

    @ApiOperation(value = "测试token", httpMethod = "POST")
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    @ResponseBody
    public R testController() {
        return R.ok("访问成功");
    }
}
